/**
 *
 */
package com.naiterui.ehp.bs.emr.controller.inner;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.emr.InquireCaseBO;
import com.naiterui.ehp.bp.bo.emr.PatientDiseaseBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.constants.SwaggerTags;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.utils.beans.BeanUtil;
import com.naiterui.ehp.bs.emr.controller.patient.EmrPatientCaseController;
import com.naiterui.ehp.bs.emr.exception.ExceptionCodes;
import com.naiterui.ehp.bs.emr.service.IPatientDiseaseService;
import com.naiterui.ehp.bs.emr.vo.patient.PatientDiseaseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
 * 患者病情信息
 * @since 2.18
 */
@Slf4j
@Validated
@Api(tags = SwaggerTag.INNER_API + SwaggerTag.MEDICAL_RECORD)
@RestController
@RequestMapping("emr/inner/patient/disease")
public class EmrInnerPatientDiseaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(EmrPatientCaseController.class);
    @Autowired
    private IPatientDiseaseService patientDiseaseService;
    @Autowired
    private IPatientDiseaseService diseaseService;

    /**
     * 获取患者病情诊断
     * @param recordId
     * @return
     */
    @RequestMapping("inquirer/case")
    public ResponseVO<InquireCaseBO> getDiagnosis(@NotNull(message = "记录ID不可为空") String recordId)
        throws BusinessException {
        LOGGER.info("获取患者病情诊断 , recordId={}", recordId);
        InquireCaseBO result = patientDiseaseService.inquirerCase(recordId);
        return PublicService.returnResponseVO(result);
    }

    /**
     * 病情列表
     * @param patientId
     * @param pageParamVO
     * @return
     * @throws BusinessException
     */
    @PostMapping("list")
    public PageVO<PatientDiseaseVO> list(Long patientId, PageParamVO pageParamVO) throws BusinessException {
        LOGGER.info("内部接口获取病情列表：patientId={}", patientId);
        return patientDiseaseService.list(patientId, pageParamVO);
    }

    /**
     * 保存病情内部接口
     * @param patientId
     * @param patientDisease
     * @return
     * @throws BusinessException
     */
    @ApiOperation(value = "保存病情内部接口", notes = "保存病情信息", tags = {SwaggerTags.WX_PATIENT_INQUIRER})
    @PostMapping("add")
    public ResponseVO<String> addPatientDisease(Long patientId, @Valid @RequestBody PatientDiseaseBO patientDisease) throws BusinessException {
        log.info("患者提交病情描述 请求参数：patientDisease = {}", patientDisease);
        patientDisease.setPatientId(patientId);
        PatientDiseaseVO patientDiseaseVO = new PatientDiseaseVO();
        BeanUtil.copyProperties(patientDisease, patientDiseaseVO);
        String patientDiseaseId = diseaseService.add(patientDiseaseVO);
        return PublicService.returnResponseVO(patientDiseaseId);
    }

    /**
     * 获取病情信息内部接口
     * @param diseaseId
     * @return
     */
    @GetMapping("get")
    public ResponseVO<PatientDiseaseBO> getPatientDisease(@NotNull(message = "病情ID不可为空") String diseaseId) {
        PatientDiseaseBO result = patientDiseaseService.getPatientDisease(diseaseId);
        return PublicService.returnResponseVO(result);
    }

    @GetMapping("info")
    public ResponseVO<PatientDiseaseBO> getDisease(Long patientId) {
        PatientDiseaseBO result = patientDiseaseService.getDisease(patientId);
        return PublicService.returnResponseVO(result);
    }

    @PostMapping("inquirer/disease/list")
    @ApiOperation("根据病情id查询病情信息")
    public List<InquireCaseBO> diseaseList(@RequestBody Set<String> diseaseIds) {
        log.info("内部接口根据病情id查询病情信息 diseaseIds:{}", diseaseIds);
        return patientDiseaseService.inquirerDiseases(diseaseIds);
    }

    @PostMapping("session/bind")
    @ApiOperation(value = "病例绑定会话", tags = SwaggerTags.WX_INNER_CASE)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "recordId", value = "记录ID", required = true, dataType = "string", paramType = "form"),
        @ApiImplicitParam(name = "sessionId", value = "会话ID", required = true, dataType = "string",
            paramType = "form")})
    public ResponseVO<Void> caseSessionBind(@NotNull(message = "记录ID不可为空") String recordId,
        @NotNull(message = "会话ID不可为空") String sessionId) throws BusinessException {
        log.info("病例绑定会话 , recordId={}, sessionId:{}", recordId, sessionId);
        patientDiseaseService.sessionBind(recordId, sessionId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }
}
